package com.maxd.service.cash;

import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.CommonInfo;
import com.maxd.model.MessageInfo;
import com.maxd.model.UserInfo;
import com.maxd.respository.cash.CashJpaRepository;
import com.maxd.respository.cash.CashRepository;
import com.maxd.respository.common.CommonRepository;
import com.maxd.respository.userinfo.UserRepository;
import com.maxd.service.message.IMessageService;
import com.maxd.service.money.IMoneyDetailsService;
import com.maxd.service.userinfo.UserServiceImpl;
import com.maxd.service.wx.AccountService;
import com.maxd.utils.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import weixin.popular.bean.message.templatemessage.TemplateMessageItem;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 提现的service实现类
 */
@Service
public class CashServiceImpl implements ICashService {
    private Logger logger = LoggerFactory.getLogger(CashServiceImpl.class);
    @Autowired
    private CashJpaRepository cashJpaRepository;
    @Autowired
    private CashRepository cashRepository;
    @Autowired
    private CommonRepository commonRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private IMoneyDetailsService iMoneyDetailsService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private IMessageService iMessageService;
    @Autowired
    private UserServiceImpl userService;

    @Override
    public Result findOne(long id) {
        return ResultUtil.success(cashJpaRepository.findById(id).orElse(null));
    }

    @Override
    public Result delete(long id) {
        cashJpaRepository.deleteById(id);
        return ResultUtil.success();
    }

    @Override
    public Result findOne(String relationId) {
        return ResultUtil.success(cashRepository.findOne(relationId));
    }

    @Override
    public Result findByPhone(String phone) {
        return ResultUtil.success(cashRepository.findByPhone(phone));
    }

    @Override
    @Transactional
    public Result updateIsOut(Boolean isOut, Long id) {
        return ResultUtil.success(cashRepository.updateIsOut(isOut, id));
    }


    //非线程安全实现，需要修改
    static class SingleByUserId {
        static Map<String, Object> map = new HashMap<>();

        static Object getById(String userId) {
            if (map.get(userId) != null) {
                return map.get(userId);
            } else {
                Object object = new Object();
                map.put(userId, object);
                return object;
            }
        }
    }

    /**
     * 结算成功通知
     *
     * @param openId
     * @param date
     * @param money
     * @param totalMoney
     * @param url
     */
    private void totalSuccess(String openId, String name, String date, String money, String totalMoney, String url) {
        UserInfo userByWxId = userRepository.getUserByWxId(openId);
        MessageInfo messageInfo = new MessageInfo();
        messageInfo.setState(String.valueOf(4));
        messageInfo.setContent(name + " 您好！您上月【确认收货订单】结算信息如下，请尽快申请提现！结算金额【" + money + "】进入【我的】点击【可提现金额】申请提现，提现周期到【下个月25号】,过期清零哦");
        messageInfo.setTitle("本月结算通知");
        messageInfo.setUserName(userByWxId.getNickName());
        messageInfo.setUserId(String.valueOf(userByWxId.getId()));
        iMessageService.saveBody(messageInfo);
        if (userByWxId.getClientid() != null) {
            userService.pushToSingle("本月结算通知", name + " 您好！您上月【确认收货订单】结算信息如下，请尽快申请提现！结算金额【" + money + "】进入【我的】点击【可提现金额】申请提现，提现周期到【下个月25号】,过期清零哦", userByWxId.getClientid());
        }

        CommonInfo three = commonRepository.findOne(37);
        String apkey = Config.JieSuanNotification;
        if (three != null) {
            apkey = three.getValue();
        }

        LinkedHashMap<String, TemplateMessageItem> data = new LinkedHashMap<>();
        data.put("first", new TemplateMessageItem(name + " 您好！您上月【确认收货订单】结算信息如下，请尽快申请提现", "#d71345"));
        data.put("keyword1", new TemplateMessageItem(date, "#d71345"));
        data.put("keyword2", new TemplateMessageItem(money + " 元", "#d71345"));
        data.put("keyword3", new TemplateMessageItem(totalMoney + " 元", "#d71345"));
        data.put("remark", new TemplateMessageItem("点击【查看详情】进入【我的】点击【可提现金额】申请提现，提现周期到【下个月25号】,过期清零哦，如有疑问请在公众号中发送您的问题联系客服", null));
        accountService.sendWxMessage(apkey, data, openId, url);
    }

}
